<template>
    <div>
        <p>
            <!--模态框(Modal)插件的触发按钮-->
            <button v-on:click="add()" class="btn btn-white btn-default btn-round">
                <i class="ace-icon fa fa-edit"></i>
                新增
            </button>
            &nbsp;
            <!--刷新按钮-->
            <button v-on:click="getList(1)" class="btn btn-white btn-default btn-round">
                <i class="ace-icon fa fa-refresh"></i>
                刷新
            </button>
        </p>
        <!--pagination可选参数：v-bind:itemCount="8" 不设置默认显示5个按钮，设置最多显示8个按钮-->
        <pagination ref="pagination" v-bind:list="getList"></pagination>

        <table id="simple-table" class="table  table-bordered table-hover">
                <thead>
                <tr>
                                    <th>登陆名</th>
                    <th>昵称</th>
                    <th>密码</th>
                    <th>操作</th>
                </tr>
                </thead>

                <tbody>

                <tr v-for="user in users">
                    <td>{{user.loginName}}</td>
                    <td>{{user.name}}</td>
                    <td>
                        <!--{{user.password}}-->
                        <input v-model="user.password" type="password" v-bind:disabled="true" class="form-control td-input">
                    </td>
                    <td>
                        <div class="hidden-sm hidden-xs btn-group">

                            <button  v-on:click="edit(user)" class="btn btn-xs btn-info">
                                <i class="ace-icon fa fa-pencil bigger-120"></i>
                            </button>
                            <!--模态框(Modal)插件的触发按钮-->
                            <button v-on:click="editPassword(user)" class="btn btn-xs btn-warning">
                                <i class="ace-icon fa fa-key"></i>
                            </button>

                            <button v-on:click="del(user.id)" class="btn btn-xs btn-danger">
                                <i class="ace-icon fa fa-trash-o bigger-120"></i>
                            </button>

                        </div>

                        <div class="hidden-md hidden-lg">
                            <div class="inline pos-rel">
                                <button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
                                    <i class="ace-icon fa fa-cog icon-only bigger-110"></i>
                                </button>

                                <ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
                                    <li>
                                        <a href="#" class="tooltip-info" data-rel="tooltip" title="View">
																			<span class="blue">
																				<i class="ace-icon fa fa-search-plus bigger-120"></i>
																			</span>
                                        </a>
                                    </li>

                                    <li>
                                        <a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
																			<span class="green">
																				<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
																			</span>
                                        </a>
                                    </li>

                                    <li>
                                        <a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
																			<span class="red">
																				<i class="ace-icon fa fa-trash-o bigger-120"></i>
																			</span>
                                        </a>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>

        <!--新增和修改的摸态框插件的实体html-->
        <div id="form-modal" class="modal fade" tabindex="-1" role="dialog">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title">表单</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">登陆名</label>
                                <div class="col-sm-10">
                                    <input v-model="user.loginName" v-bind:disabled="user.id" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">昵称</label>
                                <div class="col-sm-10">
                                    <input v-model="user.name" class="form-control">
                                </div>
                            </div>
                            <div v-if="user.id == null" class="form-group">
                                <label class="col-sm-2 control-label">密码</label>
                                <div class="col-sm-10">
                                    <input v-model="user.password" type="password" class="form-control">
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button v-on:click="save" type="button" class="btn btn-primary">保存</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->

        <!--修改密码的摸态框插件的实体html-->
        <div id="form-modal-edit-password" class="modal fade" tabindex="-1" role="dialog">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title">修改密码</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">登陆名</label>
                                <div class="col-sm-10">
                                    <input v-model="user.loginName" v-bind:disabled="user.id" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">昵称</label>
                                <div class="col-sm-10">
                                    <input v-model="user.name" v-bind:disabled="user.id" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">输入新密码</label>
                                <div class="col-sm-10">
                                    <input v-model="user.password" type="password" class="form-control" placeholder="新密码将会覆盖旧密码">
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button v-on:click="savePassword" type="button" class="btn btn-primary">保存</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->
    </div>
</template>

<script>
    import Pagination from "../../components/pagination";
    import System_Md5 from '../../../public/static/js/system_user_md5';
    export default {
        components: {System_Md5, Pagination},
        name: "system-user",
        data: function() {
            return {
                user: {},
                users: [],
            }
        },
        mounted: function() {
            // sidebar激活样式方法一
            // this.$parent.activeSidebar("system-user-sidebar");
            let _this = this;
            _this.$refs.pagination.size = 10;
            _this.getList(1);
        },
        methods: {
            /**
             * 点击【新增】
             */
            add:function() {
                let _this = this;
                _this.user = {};
                $("#form-modal").modal("show");
            },
            /**
             * 点击【编辑】
             */
            edit:function(user) {
                let _this = this;
                _this.user = $.extend({}, user);
                $("#form-modal").modal("show");
            },
            /**
             * 列表查询
             */
            getList: function (page) {
                let _this = this;
                Loading.show();
                _this.$ajax.post(process.env.VUE_APP_SERVER + '/system/admin/user/list',{
                    page: page,
                    size: _this.$refs.pagination.size,
                }).then((response)=>{
                    Loading.hide();
                    let resp = response.data;
                    if (resp.success) {
                        _this.users = resp.content.list;
                        _this.$refs.pagination.render(page, resp.content.total);
                    }
                })

            },
            /**
             * 点击【保存】
             */
            save:function() {
                let _this = this;
                //保存校验
                if (1 != 1
                    || !Validator.require(_this.user.loginName, "登陆名")
                    || !Validator.length(_this.user.loginName, "登陆名", 1, 50)
                    || !Validator.length(_this.user.name, "昵称", 1, 50)
                    || !Validator.require(_this.user.password, "密码")
                ) {
                    return;
                }

                //前后都加上一个盐值，md5加密一次
                _this.user.password = hex_md5(SALT.SALT_PREFIX + _this.user.password + SALT.SALT_SUFFIX);

                Loading.show();
                _this.$ajax.post(process.env.VUE_APP_SERVER + '/system/admin/user/save', _this.user).then((response)=>{
                    Loading.hide();
                    let resp = response.data;
                    if (resp.success) {
                        $("#form-modal").modal("hide");
                        _this.getList(1);
                        LocalStorage.remove(LOCAL_KEY_USERS);
                        Toast.success("保存成功！");
                    } else {
                        $("#form-modal").modal("hide");
                        Toast.error(resp.message);
                    }
                })
            },

            /**
             * 点击【修改密码，弹出修改密码摸态框】
             */
            editPassword: function (user) {
                let _this = this;
                _this.user = $.extend({}, user);
                _this.user.password = null;
                $("#form-modal-edit-password").modal("show");
            },

            /**
             * 点击【保存密码】
             */
            savePassword:function() {
                let _this = this;
                //保存校验
                if (1 != 1
                    || !Validator.require(_this.user.password, "密码")
                ) {
                    return;
                }

                //前后都加上一个盐值，md5加密一次
                _this.user.password = hex_md5(SALT.SALT_PREFIX + _this.user.password + SALT.SALT_SUFFIX);

                Loading.show();
                _this.$ajax.post(process.env.VUE_APP_SERVER + '/system/admin/user/save/password', _this.user).then((response)=>{
                    Loading.hide();
                    let resp = response.data;
                    if (resp.success) {
                        $("#form-modal-edit-password").modal("hide");
                        _this.getList(1);
                        Toast.success("保存成功！");
                    } else {
                        $("#form-modal-edit-password").modal("hide");
                        Toast.error(resp.message);
                    }
                })
            },

            /**
             * 点击【删除】
             */
            del(id) {
                let _this = this;
                Confirm.show("删除用户后不可恢复，确认删除？", function () {
                    Loading.show();
                    _this.$ajax.delete(process.env.VUE_APP_SERVER + '/system/admin/user/delete/' + id).then((response)=>{
                        Loading.hide();
                        let resp = response.data;
                        if (resp.success) {
                            _this.getList(1);
                            Toast.success("删除成功！");
                        }
                    })
                });
            },
        }
    }
</script>

<style scoped>
    .td-input{
        width: 50%;
    }
</style>